package Eyjafjalla.action;

import Eyjafjalla.card.status.UnknownCloud_Eyjafjalla;
import Eyjafjalla.effect.ShowCardAndAttachEffect;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;

import java.util.ArrayList;

public class CloudMelodyAction extends AbstractGameAction {
    public CloudMelodyAction(boolean isFire){
        this.isFire = isFire;
        actionType = ActionType.CARD_MANIPULATION;
        startDuration = duration = Settings.ACTION_DUR_FAST;
    }

    @Override
    public void update() {
        if(startDuration==duration){
            if(isFire){
                if(AbstractDungeon.player.drawPile.size()>0){
                    AbstractCard c;
                    for(int i =AbstractDungeon.player.drawPile.size()-1;i>=0;i--){
                        c = AbstractDungeon.player.drawPile.group.get(i);
                        if(c.type== AbstractCard.CardType.ATTACK){
                            int index = AbstractDungeon.player.drawPile.group.indexOf(c);
                            AbstractDungeon.player.drawPile.group.remove(c);
                            UnknownCloud_Eyjafjalla unknown = new UnknownCloud_Eyjafjalla();
                            unknown.setCardPreview(c);
                            unknown.current_x = c.current_x;
                            unknown.current_y = c.current_y;
                            unknown.drawScale = c.drawScale;
                            unknown.angle = c.angle;
                            AbstractDungeon.player.drawPile.group.add(index,unknown);
                            AbstractDungeon.topLevelEffectsQueue.add(new ShowCardAndAttachEffect(c.makeStatEquivalentCopy(),AbstractDungeon.player,true));
                            break;
                        }
                    }
                }
            }
            ArrayList<AbstractCard> attack = new ArrayList<>();
            for(AbstractCard c:AbstractDungeon.player.hand.group){
                if(c.type == AbstractCard.CardType.ATTACK){
                    attack.add(c);
                }
            }
            if(attack.isEmpty()){
                this.isDone = true;
                return;
            }
            else {
                for(AbstractCard c:attack){
                    c.unhover();
                    c.untip();
                    int index = AbstractDungeon.player.hand.group.indexOf(c);
                    AbstractDungeon.player.hand.group.remove(c);
                    UnknownCloud_Eyjafjalla unknown = new UnknownCloud_Eyjafjalla();
                    unknown.setCardPreview(c);
                    unknown.current_x = c.current_x;
                    unknown.current_y = c.current_y;
                    unknown.drawScale = c.drawScale;
                    unknown.angle = c.angle;
                    unknown.flash();
                    AbstractDungeon.player.hand.group.add(index,unknown);
                }
                AbstractDungeon.player.hand.refreshHandLayout();
                AbstractDungeon.player.hand.applyPowers();
            }
        }

        this.tickDuration();
    }

    boolean isFire;
}
